package com.shop2cn.iapi.sqdistributor.testcase;

import java.util.Arrays;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.sqdistributor.parameter.request.SetAgencyDistributorConfigReq;
import com.shop2cn.iapi.sqdistributor.parameter.request.pojo.AgencySubDistributorCommisionRateVo;
import com.shop2cn.iapi.sqdistributor.service.SetAgencyDistributorConfigCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.database.model.sqdistributor.DistributionAgencySubDistributorCommisionRate;
import com.ymttest.database.model.sqdistributor.DistributionAgencySubDistributorConfig;
import com.ymttest.database.sqlwapper.sqdistributor.DistributionAgencySubDistributorCommisionRateWrapper;
import com.ymttest.database.sqlwapper.sqdistributor.DistributionAgencySubDistributorConfigWrapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.TestDataManager;

public class Ts_SetAgencyDistributorConfig {

	// java接口
	private static SetAgencyDistributorConfigReq setAgencyDistributorConfigReq;
	private static SetAgencyDistributorConfigCall setAgencyDistributorConfigCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("分销机构对分销员的设置");
	}

	@Before
	public void caseUp() {
		setAgencyDistributorConfigReq = new SetAgencyDistributorConfigReq();
		setAgencyDistributorConfigCall = new SetAgencyDistributorConfigCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(setAgencyDistributorConfigCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	public void verifyDb() throws Exception {

		DistributionAgencySubDistributorConfigWrapper distributionAgencySubDistributorConfigWrapper = new DistributionAgencySubDistributorConfigWrapper();

		DistributionAgencySubDistributorConfig actualResult = distributionAgencySubDistributorConfigWrapper
				.selectByMerchantIdAndDistributorType(
						setAgencyDistributorConfigReq.getMerchantId(),
						setAgencyDistributorConfigReq.getDistributorType());

		Logger.comment("验证distribution_agency_sub_distributor_config表");

		Logger.verifyEquals(setAgencyDistributorConfigReq.getMerchantId(),
				actualResult.getMerchant_id(), "验证Merchant_id");
		Logger.verifyEquals(setAgencyDistributorConfigReq.getDistributorId(),
				actualResult.getDistributor_id(), "验证DistributorId");
		Logger.verifyEquals(setAgencyDistributorConfigReq.getDistributorType(),
				actualResult.getDistributor_type(), "验证DistributorType");

		verifyCommisionRateTable(actualResult.getId());
	}

	public void verifyCommisionRateTable(Long configId) {

		Logger.comment("验证distribution_agency_sub_distributor_commision_rate表");

		List<AgencySubDistributorCommisionRateVo> expectList = setAgencyDistributorConfigReq
				.getAgencySubDistributorCommisionRates();

		DistributionAgencySubDistributorCommisionRateWrapper distributionAgencySubDistributorCommisionRateWrapper = new DistributionAgencySubDistributorCommisionRateWrapper();

		List<DistributionAgencySubDistributorCommisionRate> actualResultList = distributionAgencySubDistributorCommisionRateWrapper
				.selectByConfigId(configId);
		
		for (int i = 0; i < actualResultList.size(); i++) {
			
			AgencySubDistributorCommisionRateVo expectResult = expectList.get(i);
			DistributionAgencySubDistributorCommisionRate actualResult = actualResultList.get(i);

			Logger.verifyEquals(setAgencyDistributorConfigReq.getMerchantId(),
					actualResult.getMerchant_id(), "验证Merchant_id");

			Logger.verifyEquals(expectResult.getDistributorLevelIndex(),
					actualResult.getDistributor_level_index(),
					"验证Distributor_level_index");
			Logger.verifyEquals(expectResult.getDistributorLevelLabel(),
					actualResult.getDistributor_level_label(),
					"验证Distributor_level_label");
			DataUtil.verifyDecimalEquals(expectResult.getLayer1Rate(),
					actualResult.getLayer1_rate(), "验证Layer1_rate");
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SetAgencyDistributorConfig_001() {
		Logger.start(true, "设置普通分销机构的分销员配置信息，设置成功");

		try {

			AgencySubDistributorCommisionRateVo agencySubDistributorCommisionRateVo = AgencySubDistributorCommisionRateVo
					.createStandardAgencySubDistributorLevelCommissionRate();

			setAgencyDistributorConfigReq.setMerchantId(500861041L);
			setAgencyDistributorConfigReq.setDistributorType(1);
			setAgencyDistributorConfigReq
					.setDistributorId((long) TestDataManager.getRandomIntNum(8));
			setAgencyDistributorConfigReq
					.setAgencySubDistributorCommisionRates(Arrays
							.asList(agencySubDistributorCommisionRateVo));

			setAgencyDistributorConfigCall
					.setData(setAgencyDistributorConfigReq);
			setAgencyDistributorConfigCall.callService();

			Logger.verifyEquals(true,
					setAgencyDistributorConfigCall.getSuccess(), "验证success");

			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SetAgencyDistributorConfig_003() {
		Logger.start(true, "MerchantId为null，设置失败");

		try {

			AgencySubDistributorCommisionRateVo agencySubDistributorCommisionRateVo = AgencySubDistributorCommisionRateVo
					.createStandardAgencySubDistributorLevelCommissionRate();

			setAgencyDistributorConfigReq.setDistributorType(1);
			setAgencyDistributorConfigReq.setDistributorId((long) TestDataManager.getRandomIntNum(8));
			setAgencyDistributorConfigReq
					.setAgencySubDistributorCommisionRates(Arrays
							.asList(agencySubDistributorCommisionRateVo));

			setAgencyDistributorConfigCall
					.setData(setAgencyDistributorConfigReq);
			setAgencyDistributorConfigCall.callService();

			Logger.verifyEquals(false,
					setAgencyDistributorConfigCall.getSuccess(), "验证success");
			Logger.verifyEquals(
					"merchantId:商户id不能为空",
					setAgencyDistributorConfigCall.getMsg(), "验证resultMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SetAgencyDistributorConfig_004() {
		Logger.start(true, "MerchantId不存在，设置成功");

		try {

			AgencySubDistributorCommisionRateVo agencySubDistributorCommisionRateVo = AgencySubDistributorCommisionRateVo
					.createStandardAgencySubDistributorLevelCommissionRate();

			setAgencyDistributorConfigReq.setMerchantId(123416443643623L);
			setAgencyDistributorConfigReq.setDistributorType(1);
			setAgencyDistributorConfigReq.setDistributorId((long) TestDataManager.getRandomIntNum(8));
			setAgencyDistributorConfigReq
					.setAgencySubDistributorCommisionRates(Arrays
							.asList(agencySubDistributorCommisionRateVo));

			setAgencyDistributorConfigCall
					.setData(setAgencyDistributorConfigReq);
			setAgencyDistributorConfigCall.callService();

			Logger.verifyEquals(true,
					setAgencyDistributorConfigCall.getSuccess(), "验证success");

			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SetAgencyDistributorConfig_005() {
		Logger.start(true, "设置普通分销机构的多条分销员配置信息，设置成功");

		try {

			AgencySubDistributorCommisionRateVo agencySubDistributorCommisionRateVo1 = AgencySubDistributorCommisionRateVo
					.createStandardAgencySubDistributorLevelCommissionRate();
			
			AgencySubDistributorCommisionRateVo agencySubDistributorCommisionRateVo2 = AgencySubDistributorCommisionRateVo
					.createStandardAgencySubDistributorLevelCommissionRate();

			setAgencyDistributorConfigReq.setMerchantId(500861041L);
			setAgencyDistributorConfigReq.setDistributorType(1);
			setAgencyDistributorConfigReq
					.setDistributorId((long) TestDataManager.getRandomIntNum(8));
			setAgencyDistributorConfigReq
					.setAgencySubDistributorCommisionRates(Arrays
							.asList(agencySubDistributorCommisionRateVo1, agencySubDistributorCommisionRateVo2));

			setAgencyDistributorConfigCall
					.setData(setAgencyDistributorConfigReq);
			setAgencyDistributorConfigCall.callService();

			Logger.verifyEquals(true,
					setAgencyDistributorConfigCall.getSuccess(), "验证success");

			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SetAgencyDistributorConfig_006() {
		Logger.start(true, "设置CPS的多条分销员配置信息，设置成功");

		try {

			AgencySubDistributorCommisionRateVo agencySubDistributorCommisionRateVo1 = AgencySubDistributorCommisionRateVo
					.createStandardAgencySubDistributorLevelCommissionRate();
			
			AgencySubDistributorCommisionRateVo agencySubDistributorCommisionRateVo2 = AgencySubDistributorCommisionRateVo
					.createStandardAgencySubDistributorLevelCommissionRate();
			
			agencySubDistributorCommisionRateVo2.setDistributorLevelIndex(1);

			setAgencyDistributorConfigReq.setMerchantId(500861041L);
			setAgencyDistributorConfigReq.setDistributorType(2);
			setAgencyDistributorConfigReq
					.setDistributorId((long) TestDataManager.getRandomIntNum(8));
			setAgencyDistributorConfigReq
					.setAgencySubDistributorCommisionRates(Arrays
							.asList(agencySubDistributorCommisionRateVo1, agencySubDistributorCommisionRateVo2));

			setAgencyDistributorConfigCall
					.setData(setAgencyDistributorConfigReq);
			setAgencyDistributorConfigCall.callService();

			Logger.verifyEquals(true,
					setAgencyDistributorConfigCall.getSuccess(), "验证success");

			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
